package com.dgshanger.sanhxiaofeisc.ui

import android.Manifest
import android.content.Intent
import android.util.Log
import android.view.KeyEvent
import com.blankj.utilcode.util.NetworkUtils
import com.blankj.utilcode.util.PermissionUtils
import com.dgshanger.sanhxiaofeisc.MainActivity
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.ui.home.utils.LocationManger
import com.dgshanger.sanhxiaofeisc.ui.login.LoginActivity
import com.xx.baseuilibrary.mvp.BaseMvpViewActivity
import java.util.*


/**
 * SplashActivity
 * 沉迷学习不能自拔
 * Describe：启动页
 * Created by 雷小星🍀 on 2018/2/1 15:42.
 */
class SplashActivity : BaseMvpViewActivity() {

    override fun openSwipeBack(): Boolean = false

    //需要检测的定位权限
    private var needPermissions = arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            /*Manifest.permission.READ_EXTERNAL_STORAGE,*/
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.CHANGE_WIFI_STATE)

    override fun getActivityLayoutId(): Int = R.layout.activity_splash

    override fun initEvent() {
        PermissionUtils.permission(needPermissions[0], needPermissions[1], needPermissions[2]
                , needPermissions[3]/*, needPermissions[4]*//*, needPermissions[5]*/)
                .callback(object : PermissionUtils.SimpleCallback {
                    override fun onGranted() {
                        Log.e("Tag", "有权限")
                        Timer().schedule(
                                object : TimerTask() {
                                    override fun run() {
                                        //延迟之后关闭当前页面
                                        LocationManger.getInstance().startLocation()
                                        if (Constants.isFirst()) {
                                            startActivityThenFinishSelf(GuideActivity::class.java)
                                        } else if (!NetworkUtils.isConnected()) {
                                            //没有网络,直接跳转到登录页面
                                            startActivityThenFinishSelf(LoginActivity::class.java)
                                        } else if (Constants.isLogin()) {
                                            val intent = Intent(mContext, MainActivity::class.java)
                                            startActivity(intent)
                                            finish()
                                        } else {
                                            startActivityThenFinishSelf(LoginActivity::class.java)
                                        }
                                        overridePendingTransition(R.anim.scale_in, R.anim.scale_out)
                                    }
                                },
                                1500
                        )
                    }

                    override fun onDenied() {
                        Log.e("Tag", "没权限")
                        //被拒绝
                        Constants.putLocation(29.575399, 106.53293, "重庆")
                        /* showToast("拒绝给予权限会导致该定位不能正常使用")*/
                        Timer().schedule(
                                object : TimerTask() {
                                    override fun run() =//延迟之后关闭当前页面
                                            if (Constants.isFirst()) {
                                                startActivityThenFinishSelf(GuideActivity::class.java)
                                            } else {
                                                val intent = Intent(mContext, MainActivity::class.java)
                                                startActivity(intent)
                                                finish()
                                                overridePendingTransition(R.anim.scale_in, R.anim.scale_out)
                                            }
                                },
                                1500
                        )
                    }

                })
                .rationale(object : PermissionUtils.OnRationaleListener {
                    override fun rationale(shouldRequest: PermissionUtils.OnRationaleListener.ShouldRequest?) {
                        shouldRequest!!.again(true)
                    }

                })
                .request()
    }

    override fun initData() {
        // 避免从桌面启动程序后，会重新实例化入口类的activity
        if (!this.isTaskRoot) { // 判断当前activity是不是所在任务栈的根
            val intent = intent
            if (intent != null) {
                val action = intent.action
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN == action) {
                    finish()
                    return
                }
            }
        }
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //禁用返回键
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}